package org.kjtc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.entity.BranchCompany;
import org.kjtc.entity.Page;
import org.kjtc.service.BranchCompanyService;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BranchCompanyController extends BaseController{

	@Resource(name="branchCompanyService")
	private BranchCompanyService  branchCompanyService;

	@RequestMapping(value="/searchBranchCompany")
	@ResponseBody
	public Map<String, Object> searchBranchCompany(BranchCompany branchCompany, String page, String rows){

		Map<String, Object> map = new HashMap<String, Object>();
	  	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司查询开始");
		// 分页
		Page pageQuery = getPage();
		pageQuery.setCurrentPage(Integer.parseInt(page));
		pageQuery.setCurrentResult((Integer.parseInt(page) - 1) * (Integer.parseInt(rows)));
		pageQuery.setShowCount(Integer.parseInt(rows));

		List<BranchCompany> list = null;
		try {
			branchCompany.setBranchCompanyID(this.getBranchCompanyID());
			list = branchCompanyService.getBranchCompanyPage(branchCompany, pageQuery);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司查询异常" + e.getMessage());
			return map;
		}
		map.put("totalCnt",  pageQuery.getTotalResult());
		map.put("totalPage", pageQuery.getTotalPage());
		map.put("branchCompanyList",list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司查询结束");
		branchCompany = null;
	    return map;
  }

  @RequestMapping(value="/saveBranchCompany")
  @ResponseBody
  public Map<String, Object> saveBranchCompany(BranchCompany branchCompany) {

		Map<String, Object> map = new HashMap<String, Object>();
	  	logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司添加开始");
		int count = 0;
		try {
			branchCompany.setCreateUser(this.getUesr().getUserID());
			branchCompany.setUpdateUser(this.getUesr().getUserID());
			count = branchCompanyService.insertBranchCompany(branchCompany);
			if(count > 0){
				map.put("result", "Success");
			}else{
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("PRIMARY".equals(error[3].trim())) {
				map.put("result", "BranchCompanyIDExist");
			} else if ("BranchCompanyName".equals(error[3].trim())){
				map.put("result", "BranchCompanyNameExist");
			}
		} catch (Exception e) {
			   e.printStackTrace();
			   map.put("result", "Error");
			   logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司添加异常" + e.getMessage());
		   }
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司添加结束");
		branchCompany = null;
		return map;
  }


	@RequestMapping(value="/updateBranchCompany")
	@ResponseBody
	public Map<String,Object> updateBranchCompany(BranchCompany branchCompany){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司修改开始");
		int count = 0;
		try {
			branchCompany.setUpdateUser(this.getUesr().getUserID());
			count = branchCompanyService.updateBranchCompany(branchCompany);
			if(count > 0){
				map.put("result", "Success");
			}else {
				map.put("result", "NotFound");
			}
		} catch (DuplicateKeyException ex) {
			String[] error = ex.getCause().getMessage().split("'");
			if ("BranchCompanyName".equals(error[3].trim())){
				map.put("result", "BranchCompanyNameExist");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司修改异常" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司修改结束");
		branchCompany = null;
		return map;
	}

	@RequestMapping(value = "/deleteBranchCompany")
	@ResponseBody
	public Map<String, Object> deleteBranchCompany(String branchCompanyIDList){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司删除开始");
		BranchCompany branchCompany = new BranchCompany();
		branchCompany.setBranchCompanyIDList(branchCompanyIDList.split(","));
		int count = 0;
		try {
			count = branchCompanyService.deleteBranchCompany(branchCompany);
			if (count > 0) {
				map.put("result", "Success");
			} else {
				map.put("result", "NotFound");
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("result", "Error");
		    logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司删除异常" + e.getMessage());
		}
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "运营公司删除结束");
		branchCompany = null;
		return map;
	}
}
